package com.jia.controller;


import com.jia.common.lang.R;
import com.jia.entity.SysUser;
import com.jia.entity.vo.LoginVo;
import com.jia.service.SysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜佳佳
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/admin/sys/user")
@Api(tags = "系统用户管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/getUserByUsername/{username}")
    public R getUserByUsername(@PathVariable String username){
        SysUser sysUser = sysUserService.getByUsername(username);
        return R.ok().data("data",sysUser);
    }



    @GetMapping("/getInfo")
    public R getInfo(LoginVo loginVo) {
        Map<String,Object> map = sysUserService.getInfo(loginVo);
        return R.ok().data(map);
    }

    @GetMapping("/list")
    public R list() {
        List<SysUser> list = sysUserService.list();
        return R.ok().data("list",list);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
        SysUser byId = sysUserService.getById(id);
        return R.ok().data("data",byId);
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id){
        sysUserService.removeById(id);
        return R.ok().message("删除成功");
    }

    @PostMapping("/save")
    public R save(@RequestBody SysUser sysUser) {
        sysUserService.save(sysUser);
        return R.ok().message("新增成功");
    }

    @PutMapping("/update")
    public R update(@RequestBody SysUser sysUser){
        sysUserService.updateById(sysUser);
        return R.ok().message("修改成功");
    }

}

